package application

import (
	"hong/uniapp_server/application/dto"
	"hong/uniapp_server/domain/diary"
	"hong/uniapp_server/domain/user"
	"time"

	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog/log"
)

type DiaryController struct {
	diaryService *diary.DiaryService
	userPhotoService *user.UserPhotoService
}

func NewDiaryController(diaryService *diary.DiaryService, userPhotoService *user.UserPhotoService) *DiaryController {
	return &DiaryController{
		diaryService: diaryService,
		userPhotoService: userPhotoService,
	}
}

func (t *DiaryController) AddDiary(c *fiber.Ctx) error {
	var data dto.DiaryRequest

	if err := c.BodyParser(&data); err != nil {
		return c.JSON(dto.Response{
			Code: dto.ResponseParamsError,
		})
	}

	day, err := time.Parse(time.DateOnly, data.Day)
	if err != nil {
		return c.JSON(dto.Response{
			Code: dto.ResponseParamsError,
		})
	}
	err = t.diaryService.AddDiary(&diary.DiaryEntity{
		UserID:  0,
		// Title:   &title,
		Day:     &day,
		Weather: &data.Weather,
		Mood:    &data.Mood,
		Content: &data.Content,
	})
	if err != nil {
		log.Error().Msgf("AddDiary error %v", err)
		return c.JSON(dto.Response{
			Code: dto.ResponseParamsError,
		})
	}

	return c.JSON(dto.Response{
		Code: dto.ResponseSuccess,
	})
}

func (t *DiaryController) UploadImg(c *fiber.Ctx) error {
	file, err := c.FormFile("file")
	if err != nil {
		return c.JSON(dto.Response{
			Code: dto.ResponseParamsError,
		})
	}

	photoPath, err := t.userPhotoService.UploadPhoto(0, file)
	if err != nil {
		log.Error().Msgf("UploadImg error %v", err)
		return c.JSON(dto.Response{
			Code: dto.ResponseParamsError,
		})
	}

	return c.JSON(dto.Response{
		Code: dto.ResponseSuccess,
		Data: photoPath,
	})
}
